﻿@using DLULearningNetwork.ViewModels;
@model DLULearningNetwork.ViewModels.VerticalMenu
<div class="MyVerticalMenu">
    <ul style="list-style: none;">
        @{
            string path = HttpContext.Current.Request.Path.ToLower();
            string[] arr = path.Split('/');
            if (arr.Length >= 3)
            {
                path = arr[1] + "/" + arr[2];
            }
        }
        @foreach (VerticalMenu menu in Model.SubMenu)
        {
            bool isActive = false;
            string submenuDisplay = "none";
            bool hasSubMenu = false;
            if (menu.SubMenu != null)
            {
                hasSubMenu = true;
                foreach (VerticalMenu m in menu.SubMenu)
                {
                    if (m.Href.ToLower().Contains(path))
                    {
                        isActive = true;
                        submenuDisplay = "block";
                        break;
                    }
                }
                if (menu.Href.ToLower().Contains(arr[1]))
                {
                    isActive = true;
                    submenuDisplay = "block";
                }
            }
            else
            {
                if (menu.Href.ToLower().Contains(path))
                {
                    isActive = true;
                    submenuDisplay = "block";
                }
            }
            if (isActive)
            {
                if (!hasSubMenu)
                {
            <li><a href="@menu.Href" class="active">@menu.DisplayedName</a></li> 
                }
                else
                {
            <li><a href="@menu.Href" class="active" hasSubMenu="true"><span style="font-size: 14px;">
                ▼</span> @menu.DisplayedName</a></li> 
                }
            }
            else
            {
                if (!hasSubMenu)
                {
            <li><a href="@menu.Href">@menu.DisplayedName</a></li>
                }
                else
                { 
            <li><a href="@menu.Href" hasSubMenu="true"><span style="font-size: 14px;">►</span> @menu.DisplayedName</a></li> 
                }
            }

            if (menu.SubMenu != null && menu.SubMenu.Count != 0)
            {
            <div class="SubMenu" style="display:@submenuDisplay">
                <ul>
                    @foreach (VerticalMenu submenu in menu.SubMenu)
                    {
                        if (submenu.Href.ToLower().Contains(path))
                        {
                        <li><a href="@submenu.Href" class="active">@submenu.DisplayedName</a></li>
                        }
                        else
                        {
                        <li><a href="@submenu.Href" >@submenu.DisplayedName</a></li>
                        }
                    }
                </ul>
            </div>
            }

        }
    </ul>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("a[hasSubMenu]").click(function () {
            $(this).parent().next().slideToggle();
            $(this).toggleClass("active");
            var curChar = $(this).find("span").text();
            if (curChar == '►') {
                $(this).find("span").first().text('▼');
            }
            else {
                $(this).find("span").first().text('►');
            }
            return false;
        });
    });
</script>
